{% set dff_api_title = "AWS-ELB Collection" %}
{% set dff_api_crontab = "* * * * *" %}
{% set obj_filters = [] %}
{% set metric_filters = [] %}

{% extends "base_template" %}
{% block import %}
{{ super() }}
import integration_aws_elb__main as obj_main
import integration_aws_cloudwatch__main as monitor_main
{% endblock %}

{% block account %}
{{ super() }}
{% endblock %}

{% block collect_configs %}
collector_configs = {
    'regions': {{ repr(collector.regions) }}
}


monitor_configs = {
    'regions': {{ repr(collector.regions) }},
    'targets': [
        {% if collector.classic_elb_metrics %}
        {
            'namespace': 'AWS/ELB',
            'metrics'  : {{ repr(collector.classic_elb_metrics) }}
        },
        {% endif %}
        {% if collector.application_elb_metrics %}
        {
            'namespace': 'AWS/ApplicationELB',
            'metrics'  : {{ repr(collector.application_elb_metrics) }}
        },
        {% endif %}
        {% if collector.network_elb_metrics %}
        {
            'namespace': 'AWS/NetworkELB',
            'metrics'  : {{ repr(collector.network_elb_metrics) }}
        },
        {% endif %}
        {% if collector.gateway_elb_metrics %}
        {
            'namespace': 'AWS/GatewayELB',
            'metrics'  : {{ repr(collector.gateway_elb_metrics) }}
        },
        {% endif %}
    ]
}

{% endblock %}

{% block filters%}
{% if collector.classic_elb_filters %}
def filter_classic_elb_instance(instance):
    '''
    return True｜False
    '''
    {% for filter in collector.classic_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
    {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
    {% set value = filter.value.split(',') %}
    {% endif %}
    if instance["Type"] != "classic":
        return True

    if {{ filter.key }}_v := instance.get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True

    {% endif %}
    {% endfor %}
    return False


def filter_classic_elb_metric(instance, namespace='AWS/ELB'):
    '''
    return True｜False
    '''
    {% for filter in collector.classic_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
        {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
        {% set value = filter.value.split(',') %}
    {% endif %}
    if {{ filter.key }}_v := instance['tags'].get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True

    {% endif %}
    {% endfor %}
    return False

{% set _ = obj_filters.append('filter_classic_elb_instance') %}
{% set _ = metric_filters.append('filter_classic_elb_metric') %}
{% endif %}

{% if collector.application_elb_filters %}
def filter_application_elb_instance(instance):
    '''
    return True｜False
    '''
    {% for filter in collector.application_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
    {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
    {% set value = filter.value.split(',') %}
    {% endif %}
    if instance["Type"] != "application":
        return True

    if {{ filter.key }}_v := instance.get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True

    {% endif %}
    {% endfor %}
    return False


def filter_application_elb_metric(instance, namespace='AWS/ApplicationELB'):
    '''
    return True｜False
    '''
    {% for filter in collector.application_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
        {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
        {% set value = filter.value.split(',') %}
    {% endif %}
    if {{ filter.key }}_v := instance['tags'].get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True

    {% endif %}
    {% endfor %}
    return False

{% set _ = obj_filters.append('filter_application_elb_instance') %}
{% set _ = metric_filters.append('filter_application_elb_metric') %}
{% endif %}

{% if collector.network_elb_filters %}
def filter_network_elb_instance(instance):
    '''
    return True｜False
    '''
    {% for filter in collector.network_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
    {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
    {% set value = filter.value.split(',') %}
    {% endif %}
    if instance["Type"] != "network":
        return True

    if {{ filter.key }}_v := instance.get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True

    {% endif %}
    {% endfor %}
    return False


def filter_network_elb_metric(instance, namespace='AWS/NetworkELB'):
    '''
    return True｜False
    '''
    {% for filter in collector.network_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
        {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
        {% set value = filter.value.split(',') %}
    {% endif %}
    if {{ filter.key }}_v := instance['tags'].get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True
    {% endif %}
    {% endfor %}
    return False

{% set _ = obj_filters.append('filter_network_elb_instance') %}
{% set _ = metric_filters.append('filter_network_elb_metric') %}
{% endif %}

{% if collector.gateway_elb_filters %}
def filter_gateway_elb_instance(instance):
    '''
    return True｜False
    '''
    {% for filter in collector.gateway_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
    {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
    {% set value = filter.value.split(',') %}
    {% endif %}
    if instance["Type"] != "gateway":
        return True

    if {{ filter.key }}_v := instance.get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True

    {% endif %}
    {% endfor %}
    return False


def filter_gateway_elb_metric(instance, namespace='AWS/GatewayELB'):
    '''
    return True｜False
    '''
    {% for filter in collector.gateway_elb_filters %}
    {% if filter.value %}
    {% if filter.option in ["=="] %}
        {% set value = filter.value %}
    {% elif  filter.option in ["in"] %}
        {% set value = filter.value.split(',') %}
    {% endif %}
    if {{ filter.key }}_v := instance['tags'].get({{ repr(filter.key) }}):
        if {{ filter.key }}_v {{filter.option}} {{repr(value)}}:
            return True
    {% endif %}
    {% endfor %}
    return False

{% set _ = obj_filters.append('filter_gateway_elb_instance') %}
{% set _ = metric_filters.append('filter_gateway_elb_metric') %}
{% endif %}
{% endblock %}

{% block run %}
@DFF.API('{{ dff_api_title }}', timeout=300, fixed_crontab='{{ dff_api_crontab }}')
def run():
    collectors = [
        {% if obj_filters %}
        obj_main.DataCollector(account, collector_configs, filters=[{{ obj_filters | join(', ') }}]),
        {% else %}
        obj_main.DataCollector(account, collector_configs),
        {% endif %}
        {% if metric_filters %}
        monitor_main.DataCollector(account, monitor_configs, filters=[{{ metric_filters | join(', ') }}])
        {% else %}
        monitor_main.DataCollector(account, monitor_configs)
        {% endif %}
    ]
    Runner(collectors).run()
{% endblock %}